from connect.conn_mysql import Connect
from account_numbers.register import SaveCodes

class Update(Connect):

    def insert_code(self,phone,phone_verify_code):
        sc = SaveCodes()
        sc.save_codes(phone,phone_verify_code)

    def updates(self,phone,password):
        update_sql = '''
        update user set password = %s where phone = %s
        '''
        result = self.update(update_sql,(password,phone))
        return result

    def verify_codes_fun(self):
        verify_code_sql = '''
        select phone_verify_code from verify_code where id=(select MAX(id) from verify_code )
        '''
        verify_code_result = self.select_one(verify_code_sql)
        return verify_code_result

    def update_pwd(self,phone,phone_verify_code,password):
        select_user_sql = '''
        select * from user where phone = %s
        '''    
        select_user_result = self.select_one(select_user_sql,(phone,))
        print(select_user_result)
        if select_user_result == None:
            return{
                    'status':-1,
                    'errorCode':404,
                    'errorMsg':'请检查你的号码是否正确',
                    'resultBody':'你的号码还没有注册,或者号码输入有误'
                }
        if select_user_result:
            result = self.verify_codes_fun()
            if result:
                su = self.updates(phone,password) 
                print(su)             
                return{
                    'status':1,
                    'errorCode':None,
                    'errorMsg':None,
                    'resultBody':'修改密码成功'
                    }
            else:return{
                    'status':-1,
                    'errorCode':405,
                    'errorMsg':'验证码不正确',
                    'resultBody':'请检查你的验证码'
                    }
        
        
            
            


        

